global infile "DataMain.dta"
global outfile "$results/Table 5"


* Table 5A. Pre-post data and estimates

use "$infile", clear
gen post = 0
replace post = 1 if week >= 0 & weeksDD == 1
replace post = 1 if week2 >= 0 & weeksDD == 2
egen datasetpost = group(dataset post)
egen waterdatasetpost = mean(water), by(home datasetpost)
gen logwaterdatasetpost = log(waterdatasetpost+1)
gen logwaterdatasetpost0 = log(waterdatasetpost)
gen dwaterdatasetpost = (waterdatasetpost != 0)
replace water = waterdatasetpost
replace logwater = logwaterdatasetpost
replace logwater0 = logwaterdatasetpost0
replace dwater = dwaterdatasetpost
duplicates drop home datasetpost, force
gen zwater = 1-dwater
sum zwater if dupindicator == 0
save "Temp", replace

use "Temp", clear
reghdfe logwater treat, absorb(datasethome datasetpost) cluster(home)
leveleffects
outreg2 using "$outfile", keep(treat) nocon 2aster dec(3) word replace ///
addstat(pre, $prem, effect, $effectm, transbeta, $transbeta) 

use "Temp", clear 
drop if logwater0 == .
reghdfe logwater0 treat, absorb(datasethome datasetpost) cluster(home)
leveleffects
outreg2 using "$outfile", keep(treat) nocon 2aster dec(3) word ap ///
addstat(pre, $prem, effect, $effectm, transbeta, $transbeta) 

use "Temp", clear 
reghdfe dwater treat, absorb(datasethome datasetpost) cluster(home)
leveleffects
outreg2 using "$outfile", keep(treat) nocon 2aster dec(3) word ap ///
addstat(pre, $prem, effect, $effectm, transbeta, $transbeta) 


* Table 5B. Monthly data and estimates

use "$infile", clear
egen datasetmonth = group(dataset month)
egen watermonth = mean(water), by(home month)
gen logwatermonth = log(watermonth+1)
gen logwatermonth0 = log(watermonth)
gen dwatermonth = (watermonth != 0)
replace water = watermonth
replace logwater = logwatermonth
replace logwater0 = logwatermonth0
replace dwater = dwatermonth
duplicates drop dataset home month, force
gen zwater = 1-dwater
sum zwater if dupindicator == 0
save "Temp", replace

use "Temp", clear
reghdfe logwater treat, absorb(datasethome datasetmonth) cluster(home)
leveleffects
outreg2 using "$outfile", keep(treat) nocon 2aster dec(3) word ap ///
addstat(pre, $prem, effect, $effectm, transbeta, $transbeta) 

use "Temp", clear 
drop if logwater0 == .
reghdfe logwater0 treat, absorb(datasethome datasetmonth) cluster(home)
leveleffects
outreg2 using "$outfile", keep(treat) nocon 2aster dec(3) word ap ///
addstat(pre, $prem, effect, $effectm, transbeta, $transbeta) 

use "Temp", clear 
reghdfe dwater treat, absorb(datasethome datasetmonth) cluster(home)
keep if (cohort == 1 & weeksDD == 1 & treat == 0) | (cohort != 1 & weeksDD == 2 & treat == 0)
leveleffects
outreg2 using "$outfile", keep(treat) nocon 2aster dec(3) word ap ///
addstat(pre, $prem, effect, $effectm, transbeta, $transbeta) 


* Table 5C. Weekly data and estimates

use "$infile", clear
reghdfe logwater treat, absorb(datasethome datasetweek) cluster(home)
leveleffects
outreg2 using "$outfile", keep(treat) nocon 2aster dec(3) word ap ///
addstat(pre, $prem, effect, $effectm, transbeta, $transbeta) 

use "$infile", clear 
sum logwater0
drop if logwater0 == .
reghdfe logwater0 treat, absorb(datasethome datasetweek) cluster(home)
leveleffects
outreg2 using "$outfile", keep(treat) nocon 2aster dec(3) word ap ///
addstat(pre, $prem, effect, $effectm, transbeta, $transbeta) 

use "$infile", clear 
gen zwater = 1-dwater
sum zwater if dupindicator == 0
reghdfe dwater treat, absorb(datasethome datasetweek) cluster(home)
leveleffects
outreg2 using "$outfile", keep(treat) nocon 2aster dec(3) word ap ///
addstat(pre, $prem, effect, $effectm, transbeta, $transbeta) 


* Table 5D. Daily data and estimates

use "DataDaily", clear
egen datasetdate = group(dataset date)
replace water = wpd
replace logwater = log(water+1)
replace logwater0 = log(water)
replace dwater = (water != 0)
gen zwater = 1 - dwater
sum zwater if dupindicator == 0
save "Temp", replace

use "Temp", clear
reghdfe logwater treat, absorb(datasethome datasetdate) cluster(home)
leveleffects
outreg2 using "$outfile", keep(treat) nocon 2aster dec(3) word ap ///
addstat(pre, $prem, effect, $effectm, transbeta, $transbeta) 

use "Temp", clear 
drop if logwater0 == .
reghdfe logwater0 treat, absorb(datasethome datasetdate) cluster(home)
leveleffects
outreg2 using "$outfile", keep(treat) nocon 2aster dec(3) word ap ///
addstat(pre, $prem, effect, $effectm, transbeta, $transbeta) 

use "Temp", clear 
reghdfe dwater treat, absorb(datasethome datasetdate) cluster(home)
leveleffects
outreg2 using "$outfile", keep(treat) nocon 2aster dec(3) word ap ///
addstat(pre, $prem, effect, $effectm, transbeta, $transbeta) 

